﻿using Microsoft.Data.Sqlite;
using project.B12.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project.B12.Repository.Base
{
    public class SugarDbContext
    {
        public static string ConnStr = new SqliteConnectionStringBuilder(DataPath.DataConnectionString)
        {
            Mode = SqliteOpenMode.ReadWriteCreate,
            //Mode = SqliteOpenMode.ReadWrite,
            //Password = "admin"
        }.ToString();

        public static string ConnStrHis = new SqliteConnectionStringBuilder(DataPath.DataHistoryString)
        {
            Mode = SqliteOpenMode.ReadWriteCreate,
            //Mode = SqliteOpenMode.ReadWrite,
            //Password = "admin"
        }.ToString();



        public SqlSugarClient DB = new SqlSugarClient(new List<ConnectionConfig>()
        {
            new ConnectionConfig()
            {
                ConfigId="0",
                ConnectionString = ConnStr,//必填, 数据库连接字符串
                DbType = DbType.Sqlite,//必填, 数据库类型
                IsAutoCloseConnection = false,//默认false, 自动释放和关闭数据库连接，如果有事务事务结束时关闭，否则每次操作后关闭
                InitKeyType = InitKeyType.Attribute,//默认SystemTable, 字段信息读取, 如：该属性是不是主键，标识列等等信息

                ConfigureExternalServices = new ConfigureExternalServices
                {
                //注意:  这儿AOP设置不能少
                    EntityService = (c, p) =>
                    {
                    // int?  decimal?这种 isnullable=true 不支持string
                    //if (c.PropertyType.IsGenericType &&
                    //    c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    //{
                    //    p.IsNullable = true;
                    //}

                    //// 高版C#写法 支持string?和string
                    //if (new NullabilityInfoContext()
                    //.Create(c).WriteState is NullabilityState.Nullable)
                    //{
                    //    p.IsNullable = true;
                    //}
                    }
                }
            },
            new ConnectionConfig()
            {
                ConfigId="1",
                ConnectionString = ConnStrHis,//必填, 数据库连接字符串
                DbType = DbType.Sqlite,//必填, 数据库类型
                IsAutoCloseConnection = false,//默认false, 自动释放和关闭数据库连接，如果有事务事务结束时关闭，否则每次操作后关闭
                InitKeyType = InitKeyType.Attribute,//默认SystemTable, 字段信息读取, 如：该属性是不是主键，标识列等等信息

                ConfigureExternalServices = new ConfigureExternalServices
                {
                //注意:  这儿AOP设置不能少
                    EntityService = (c, p) =>
                    {
                    // int?  decimal?这种 isnullable=true 不支持string
                    //if (c.PropertyType.IsGenericType &&
                    //    c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    //{
                    //    p.IsNullable = true;
                    //}

                    //// 高版C#写法 支持string?和string
                    //if (new NullabilityInfoContext()
                    //.Create(c).WriteState is NullabilityState.Nullable)
                    //{
                    //    p.IsNullable = true;
                    //}
                    }
                }
            }

        });

       
    }
}
